package com.b2b.service.impl;

import com.alipay.api.*;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.b2b.config.AlipayConfig;
import com.b2b.model.common.OrderPay;
import com.b2b.service.AlipayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;


@Service
@Slf4j
public class AlipayServiceImpl implements AlipayService {


    @Override
    public String aliPay(OrderPay orderPay, HttpServletResponse response, HttpServletRequest request) throws Exception {
        response.setContentType("text/html;charset=utf-8");

        //将数据进行插入到订单表中


        //调用支付宝支付
        PrintWriter out = response.getWriter();
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
        //设置请求参数
        AlipayTradePagePayRequest aliPayRequest = new AlipayTradePagePayRequest();
        aliPayRequest.setReturnUrl(AlipayConfig.return_url);
//        aliPayRequest.setNotifyUrl(AlipayConfig.notify_url);

        //商户订单号，后台可以写一个工具类生成一个订单号，必填
        String order_number = new String(System.currentTimeMillis()+"");
        //付款金额，从前台获取，必填
        String total_amount = orderPay.getMoney();
        //订单名称，必填
        String subject = new String("订单");

        aliPayRequest.setBizContent("{\"out_trade_no\":\"" + orderPay.getOrderId() + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求
        String result = null;

        result = alipayClient.pageExecute(aliPayRequest).getBody();

        //输出
        out.println(result);
        log.info("返回结果={}",result);
        return result;
    }

    @Override
//    public String aliPay(String money, HttpServletResponse response, HttpServletRequest request) throws Exception {
    public String aliTransfer(OrderPay orderPay, String email, String name, HttpServletResponse response, HttpServletRequest request) throws Exception {
        response.setContentType("text/html;charset=utf-8");


        //构造client
        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
        certAlipayRequest.setServerUrl(AlipayConfig.gateway);
        certAlipayRequest.setAppId(AlipayConfig.app_id);
        certAlipayRequest.setPrivateKey(AlipayConfig.private_key);
        certAlipayRequest.setFormat("json");
        certAlipayRequest.setCharset(AlipayConfig.charset);
        certAlipayRequest.setSignType(AlipayConfig.sign_type);
        certAlipayRequest.setCertPath(AlipayConfig.app_cert_path);
        certAlipayRequest.setAlipayPublicCertPath(AlipayConfig.alipay_cert_path);
        certAlipayRequest.setRootCertPath(AlipayConfig.alipay_root_cert_path);
        DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);

        AlipayFundTransUniTransferRequest fundTransferReq = new AlipayFundTransUniTransferRequest();
        String out_biz_no = System.currentTimeMillis()+"";

        fundTransferReq.setBizContent("{\"out_biz_no\":\"" + out_biz_no + "\","
        + "\"trans_amount\":\"" + orderPay.getMoney() + "\","
        + "\"product_code\":\"TRANS_ACCOUNT_NO_PWD\","
        + "\"biz_scene\":\"DIRECT_TRANSFER\","
        + "\"payee_info\":{" 
        + "\"identity\":\"" + email + "\","
        + "\"identity_type\":\"ALIPAY_LOGON_ID\","
        + "\"name\":\"" + name +"\"},"
        + "\"remark\":\"单笔转账\""
        
        + "}");

        AlipayFundTransUniTransferResponse fundTransferResponse = alipayClient.certificateExecute(fundTransferReq);

        System.out.println(alipayClient.pageExecute(fundTransferReq,"GET").getBody());
        if(fundTransferResponse.isSuccess()){
        System.out.println("test:AlipayTradeRefund调用成功");
        } else {
        System.out.println("test:AlipayTradeRefund调用失败");
        }
        return "1";

    }
}
